/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.restclient.dialogs;

import cz.muni.fi.pa165.machineryrental.dto.CustomerDTO;
import cz.muni.fi.pa165.machineryrental.dto.CustomerType;
import cz.muni.fi.pa165.machineryrental.restclient.services.CustomerRestService;
import cz.muni.fi.pa165.machineryrental.restclient.services.CustomerRestServiceImpl;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author David Katuscak, 359273
 */
public class CustomerDialog extends javax.swing.JDialog {

    private CustomerDTO customer;
    private boolean dialogResult;
    private boolean editable;
    private final static String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    private final static String PHONE_PATTERN = "^\\u002b\\d\\d\\d \\d\\d\\d \\d\\d\\d \\d\\d\\d$";

    /**
     * Creates new form AddNewMachineDialog
     */
    public CustomerDialog(java.awt.Frame parent, boolean modal, CustomerDTO customer, boolean editable) {

        super(parent, modal);
        initComponents();

        if (customer == null) {
            throw new NullPointerException("customer is null");
        }

        this.editable = editable;
        this.customer = customer;
        setValues(customer);

        headerLabel.setText(customer.getId() <= 0 ? "Add customer" : "Edit customer");
        email.setEditable(editable);
        customerType.setEditable(editable);
        vatNumber.setEditable(editable);
        companyNumber.setEditable(editable);
    }

    public CustomerDialog(CustomerDTO customer, boolean editable) {
        super();
        initComponents();

        if (customer == null) {
            throw new NullPointerException("customer is null");
        }

        this.editable = editable;

        this.customer = customer;
        setValues(customer);

        this.headerLabel.setText(customer.getId() <= 0 ? "Add customer" : "Edit customer");
        email.setEditable(editable);
        customerType.setEditable(editable);
        vatNumber.setEditable(editable);
        companyNumber.setEditable(editable);
    }

    public boolean getDialogResult() {
        return this.dialogResult;
    }

    private boolean validatePattern(String regex, String value) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }

    private void setValues(CustomerDTO customer) {
        firstName.setText(customer.getFirstName());
        lastName.setText(customer.getLastName());
        email.setText(customer.getEmail());
        phone.setText(customer.getPhone());
        street.setText(customer.getStreet());
        city.setText(customer.getCity());
        zipcode.setText(customer.getZipCode());
        country.setText(customer.getCountry());

        if (customer.getCustomerType() == CustomerType.PERSONAL) {
            customerType.setSelectedIndex(0);
        } else {
            customerType.setSelectedIndex(1);
        }

        vatNumber.setText(customer.getVatNumber());
        companyNumber.setText(customer.getCompanyNumber());
    }

    private boolean validateForm(CustomerRestService service) {
        boolean hasError = false;

        if (firstName.getText() == null || firstName.getText().isEmpty()) {
            firstNameErrLbl.setText("First name is required");
            hasError = true;
        } else {
            firstNameErrLbl.setText(" ");
        }

        if (lastName.getText() == null || lastName.getText().isEmpty()) {
            lastNameErrLbl.setText("Last name is required");
            hasError = true;
        } else {
            lastNameErrLbl.setText(" ");
        }

        if (phone.getText() == null || phone.getText().isEmpty()) {
            phoneErrLbl.setText("Phone is required");
            hasError = true;
        } else if (!this.validatePattern(PHONE_PATTERN, phone.getText())) {
            phoneErrLbl.setText("Required format of phone number is +xxx xxx xxx xxx");
            hasError = true;
        } else {
            phoneErrLbl.setText(" ");
        }

        if (street.getText() == null || street.getText().isEmpty()) {
            streetErrLbl.setText("Street is required");
            hasError = true;
        } else {
            streetErrLbl.setText(" ");
        }

        if (city.getText() == null || city.getText().isEmpty()) {
            cityErrLbl.setText("City is required");
            hasError = true;
        } else {
            cityErrLbl.setText(" ");
        }

        if (zipcode.getText() == null || zipcode.getText().isEmpty()) {
            zipcodeErrLbl.setText("ZIP code is required");
            hasError = true;
        } else {
            zipcodeErrLbl.setText(" ");
        }

        if (country.getText() == null || country.getText().isEmpty()) {
            countryErrLbl.setText("Country is required");
            hasError = true;
        } else {
            countryErrLbl.setText(" ");
        }


        if (editable) {
            if (email.getText() == null || email.getText().isEmpty()) {
                emailErrLbl.setText("Email is required");
                hasError = true;
            } else if (!this.validatePattern(EMAIL_PATTERN, email.getText())) {
                emailErrLbl.setText("Email is not in valid format");
                hasError = true;
            } else if (!service.isEmailUnique(email.getText())) {
                emailErrLbl.setText("Given email is already used");
                hasError = true;
            } else {
                emailErrLbl.setText(" ");
            }

            String type = (String) customerType.getSelectedItem();

            if (!(type.equals("PERSONAL") || type.equals("BUSINESS"))) {
                typeErrLbl.setText("Customer type has to be PERSONAL or BUSINESS only");
                hasError = true;
            } else {
                typeErrLbl.setText(" ");
            }

            if (type.equals("BUSINESS")) {
                if (vatNumber.getText() == null || vatNumber.getText().isEmpty()) {
                    vatErrLbl.setText("VAT number for BUSINESS customer is required");
                    hasError = true;
                } else if (!service.isVATUnique(vatNumber.getText())) {
                    vatErrLbl.setText("Given VAT number is already used");
                    hasError = true;
                } else {
                    vatErrLbl.setText(" ");
                }

                if (companyNumber.getText() == null || companyNumber.getText().isEmpty()) {
                    companyNoErrLbl.setText("Company No. for BUSINESS customer is required");
                    hasError = true;
                } else if (!service.isCompanyNumberUnique(companyNumber.getText())) {
                    companyNoErrLbl.setText("Given company number is already used");
                    hasError = true;
                } else {
                    companyNoErrLbl.setText(" ");
                }
            } else {
                vatErrLbl.setText(" ");
                companyNoErrLbl.setText(" ");
            }
        }

        return hasError;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        firstName = new javax.swing.JTextField();
        headerLabel = new javax.swing.JLabel();
        firstNameLabel = new javax.swing.JLabel();
        lastNameLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        phoneLabel = new javax.swing.JLabel();
        streetLabel = new javax.swing.JLabel();
        cityLabel = new javax.swing.JLabel();
        zipcodeLabel = new javax.swing.JLabel();
        countryLabel = new javax.swing.JLabel();
        typeLabel = new javax.swing.JLabel();
        vatLabel = new javax.swing.JLabel();
        companyNumberLabel = new javax.swing.JLabel();
        submitBtn = new javax.swing.JButton();
        lastName = new javax.swing.JTextField();
        email = new javax.swing.JTextField();
        phone = new javax.swing.JTextField();
        street = new javax.swing.JTextField();
        city = new javax.swing.JTextField();
        zipcode = new javax.swing.JTextField();
        country = new javax.swing.JTextField();
        customerType = new javax.swing.JComboBox();
        vatNumber = new javax.swing.JTextField();
        companyNumber = new javax.swing.JTextField();
        cancelBtn = new javax.swing.JButton();
        firstNameErrLbl = new javax.swing.JLabel();
        lastNameErrLbl = new javax.swing.JLabel();
        emailErrLbl = new javax.swing.JLabel();
        phoneErrLbl = new javax.swing.JLabel();
        streetErrLbl = new javax.swing.JLabel();
        cityErrLbl = new javax.swing.JLabel();
        zipcodeErrLbl = new javax.swing.JLabel();
        countryErrLbl = new javax.swing.JLabel();
        typeErrLbl = new javax.swing.JLabel();
        vatErrLbl = new javax.swing.JLabel();
        companyNoErrLbl = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add New Customer");
        setResizable(false);

        headerLabel.setText("Add new customer");

        firstNameLabel.setText("First name *");

        lastNameLabel.setText("Last name *");

        emailLabel.setText("Email *");

        phoneLabel.setText("Phone *");

        streetLabel.setText("Street *");

        cityLabel.setText("City *");

        zipcodeLabel.setText("ZIP Code *");

        countryLabel.setText("Country *");

        typeLabel.setText("Type *");

        vatLabel.setText("VAT");

        companyNumberLabel.setText("Company number");

        submitBtn.setText("Submit");
        submitBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                submitBtnMouseClicked(evt);
            }
        });

        customerType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PERSONAL", "BUSINESS" }));

        cancelBtn.setText("Cancel");
        cancelBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelBtnMouseClicked(evt);
            }
        });

        firstNameErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        firstNameErrLbl.setText(" ");

        lastNameErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        lastNameErrLbl.setText(" ");

        emailErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        emailErrLbl.setText(" ");

        phoneErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        phoneErrLbl.setText(" ");

        streetErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        streetErrLbl.setText(" ");

        cityErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        cityErrLbl.setText(" ");

        zipcodeErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        zipcodeErrLbl.setText(" ");

        countryErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        countryErrLbl.setText(" ");

        typeErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        typeErrLbl.setText(" ");

        vatErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        vatErrLbl.setText(" ");

        companyNoErrLbl.setForeground(new java.awt.Color(255, 0, 51));
        companyNoErrLbl.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(178, 178, 178)
                .addComponent(headerLabel)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(emailLabel)
                    .addComponent(lastNameLabel)
                    .addComponent(firstNameLabel)
                    .addComponent(phoneLabel)
                    .addComponent(streetLabel)
                    .addComponent(cityLabel)
                    .addComponent(zipcodeLabel)
                    .addComponent(countryLabel)
                    .addComponent(typeLabel)
                    .addComponent(vatLabel)
                    .addComponent(companyNumberLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(phoneErrLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(emailErrLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(submitBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(cancelBtn))
                            .addComponent(email)
                            .addComponent(lastName)
                            .addComponent(firstName, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                            .addComponent(firstNameErrLbl, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lastNameErrLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(37, 37, 37))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(streetErrLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(phone))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(companyNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(vatNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(customerType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(vatErrLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(companyNoErrLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(typeErrLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(countryErrLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(zipcodeErrLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cityErrLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(country)
                                .addComponent(zipcode)
                                .addComponent(city)
                                .addComponent(street)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {city, companyNumber, country, customerType, email, firstName, lastName, phone, street, vatNumber, zipcode});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelBtn, submitBtn});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headerLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(firstNameErrLbl)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(firstNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lastNameErrLbl)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastNameLabel))
                .addGap(2, 2, 2)
                .addComponent(emailErrLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(emailLabel))
                .addGap(1, 1, 1)
                .addComponent(phoneErrLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(phoneLabel))
                .addGap(1, 1, 1)
                .addComponent(streetErrLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(street, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(streetLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cityErrLbl)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(city, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cityLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(zipcodeErrLbl)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zipcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(zipcodeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(countryErrLbl)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(country, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(countryLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(typeErrLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(customerType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(typeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vatErrLbl)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vatNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vatLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(companyNoErrLbl)
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(companyNumberLabel))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(submitBtn)
                    .addComponent(cancelBtn))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cityLabel, companyNumberLabel, countryLabel, emailLabel, firstNameLabel, headerLabel, lastNameLabel, phoneLabel, streetLabel, typeLabel, vatLabel, zipcodeLabel});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {city, companyNumber, country, customerType, email, firstName, lastName, phone, street, vatNumber, zipcode});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cancelBtn, submitBtn});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelBtnMouseClicked
        this.dispose();
    }//GEN-LAST:event_cancelBtnMouseClicked

    private void submitBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_submitBtnMouseClicked

        CustomerRestService service = new CustomerRestServiceImpl();

        boolean hasError = validateForm(service);

        if (hasError) {
            return;
        }

        customer.setFirstName(firstName.getText());
        customer.setLastName(lastName.getText());
        customer.setEmail(email.getText());
        customer.setPhone(phone.getText());
        customer.setStreet(street.getText());
        customer.setCity(city.getText());
        customer.setZipCode(zipcode.getText());
        customer.setCountry(country.getText());
        if (customerType.getSelectedIndex() == 0) {
            customer.setCustomerType(CustomerType.PERSONAL);
        } else {
            customer.setCustomerType(CustomerType.BUSINESS);
        }
        customer.setVatNumber(vatNumber.getText());
        customer.setCompanyNumber(companyNumber.getText());

        dialogResult = true;
        dispose();
    }//GEN-LAST:event_submitBtnMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JTextField city;
    private javax.swing.JLabel cityErrLbl;
    private javax.swing.JLabel cityLabel;
    private javax.swing.JLabel companyNoErrLbl;
    private javax.swing.JTextField companyNumber;
    private javax.swing.JLabel companyNumberLabel;
    private javax.swing.JTextField country;
    private javax.swing.JLabel countryErrLbl;
    private javax.swing.JLabel countryLabel;
    private javax.swing.JComboBox customerType;
    private javax.swing.JTextField email;
    private javax.swing.JLabel emailErrLbl;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JTextField firstName;
    private javax.swing.JLabel firstNameErrLbl;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JTextField lastName;
    private javax.swing.JLabel lastNameErrLbl;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JTextField phone;
    private javax.swing.JLabel phoneErrLbl;
    private javax.swing.JLabel phoneLabel;
    private javax.swing.JTextField street;
    private javax.swing.JLabel streetErrLbl;
    private javax.swing.JLabel streetLabel;
    private javax.swing.JButton submitBtn;
    private javax.swing.JLabel typeErrLbl;
    private javax.swing.JLabel typeLabel;
    private javax.swing.JLabel vatErrLbl;
    private javax.swing.JLabel vatLabel;
    private javax.swing.JTextField vatNumber;
    private javax.swing.JTextField zipcode;
    private javax.swing.JLabel zipcodeErrLbl;
    private javax.swing.JLabel zipcodeLabel;
    // End of variables declaration//GEN-END:variables
}
